package org.jeecg.modules.quartz.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.nc.enums.WSEnum;
import org.jeecg.common.nc.service.NCWebService;
import org.jeecg.common.nc.vo.WSVO;
import org.jeecg.modules.nc.entity.NcMaterial;
import org.jeecg.modules.nc.service.INcMaterialService;
import org.jeecg.modules.quartz.entity.TbJobLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ITbJobLogService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

/**
 * NC物料同步
 */
public class NCMaterialSyncJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(NCMaterialSyncJob.class);

    // 物料同步
    @Autowired
    private INcMaterialService ncMaterialService;

    @Autowired
    IQuartzJobService jobService;

    @Autowired
    private ITbJobLogService tbJobLogService;

    @Value("${qixin.ncserver.uri}")
    private String NC_SERVER_URI;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        String lastTime = jobService.queryLastTime("ncmaterialsyncjob");
        String currentTime = DateUtil.now();
        Boolean first = false;
        if (StrUtil.isBlank(lastTime)){
            lastTime = "2008-01-01 00:00:00";
            first = true; // 表示第一次同步
        }

        // 获取NC组织数据
        try {
            WSVO vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.SHANGPIN,lastTime);
            for (Map<String, Object> map : vo.getContent()) {
                NcMaterial ncMaterial = new NcMaterial();
                ncMaterial.setSid((String) map.get("pk_material"));
                ncMaterial.setCode((String) map.get("code"));
                ncMaterial.setName((String) map.get("name"));
                ncMaterial.setMaterialspec((String)map.get("materialspec")); //规格
                ncMaterial.setMaterialtype((String)map.get("pk_brand")); //品牌
                ncMaterial.setPkMeasdoc((String)map.get("pk_measdoc")); //计量单位
                ncMaterial.setPkMarbasclass((String)map.get("pk_marbasclass")); //物料分类
                ncMaterial.setDef8((String)map.get("def8")); //是否扶贫
                ncMaterial.setDef9((String)map.get("def9")); //是否定制
                ncMaterial.setDr((Integer) map.get("dr"));
                ncMaterial.setEnablestate((Integer)map.get("enablestate")); //启用状态
                ncMaterial.setUpdateTime(new Date());
                ncMaterialService.saveOrUpdate(ncMaterial);
            }
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC物料同步");
            tbJobLog.setContent("本次新增或更新记录" + vo.getContent().size() + "条");
            tbJobLog.setNum(vo.getContent().size());
            tbJobLogService.save(tbJobLog);
            if(first){
                // 新增一条记录
                jobService.insetLastTime("ncmaterialsyncjob", currentTime);
            }else{
                //更新最后同步时间
                jobService.updateLastTime("ncmaterialsyncjob", currentTime);
            }
            logger.info("NC物料同步成功更新：{}条", vo.getContent().size());
        } catch (Exception e) {
            logger.info("NC物料同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC物料同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }

    }
}
